/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <android/log.h>

// ks pro..
#include "draw/egl/EGL_Base.h"
#include "draw/gles2/GLES2_Base.h"
#include "draw/gles1/GLES1_Base.h"
#include "util/log/Android_Log.h"
#include "util/static/Static_APP.h"

/* Set to 1 to enable debug log traces. */
#define DEBUG 0

/* Set to 1 to optimize memory stores when generating plasma. */
#define OPTIMIZE_WRITES  1

EGL_Base eglBase;
GLES2_Base gles2Base;
GLES1_Base gles1Base;

// data

TwoD_Manage* twod_manage;


/**
 * Process the next main command.
 */
static void app_handle_cmd(struct android_app* a_sApp, int32_t cmd) {
    
    switch (cmd) {
        case APP_CMD_SAVE_STATE:
            // The system has asked us to save our current state.  Do so.
            break;
        case APP_CMD_INIT_WINDOW:
            // The window is being shown, get it ready.
            if (a_sApp->window != NULL) {
		eglBase.initDisplay();
		eglBase.initWindowSurface(a_sApp);
		eglBase.makeCurrent();

		LOGI(" Before set static app \n");
		// store global static data
		Static_APP::s_sApp = a_sApp;
		twod_manage = new TwoD_Manage();
		twod_manage->initTexLoader();
		LOGI(" after init Tex \n");
		gles1Base.gles1_loadTexture();
		
            }
            break;
        case APP_CMD_TERM_WINDOW:
            // The window is being hidden or closed, clean it up.
            eglBase.Release();
            break;
        case APP_CMD_GAINED_FOCUS:
            // When our app gains focus, we start monitoring the accelerometer.
	    	gles1Base.gles1_drawTexture();
		//gles2Base.draw();
		eglBase.swapBuffer();
            break;
        case APP_CMD_LOST_FOCUS:
            // When our app loses focus, we stop monitoring the accelerometer.
            // This is to avoid consuming battery while not being used.
            break;
    }
}


void android_main(struct android_app* a_sApp) {
    struct android_poll_source* source;
    // Make sure glue isn't stripped.
    app_dummy();

    int events, ident;
    int _nSign = 0;
    int k = 0;
    
    a_sApp->onAppCmd = app_handle_cmd;
    
    while(1)
    {
	
	    while ((ident = ALooper_pollAll(0, NULL, &events,
		        (void**)&source)) >= 0) {

		if (source != NULL) {
		        source->process(a_sApp, source);
		    }
	    }
	    
	    //LOGI("Entrance sta:%d des:%d run:%d ident:%d \n", a_sApp->activityState, a_sApp->destroyRequested, a_sApp->running, ident);
	    //LOGI("Entrance stas:%d dest:%d red:%d \n", a_sApp->stateSaved, a_sApp->destroyed, a_sApp->redrawNeeded);
	    
	    // Check if we are exiting.
            if (a_sApp->destroyRequested != 0)
	    {
                eglBase.Release();
                return;
            }
	    
	    //LOGI("Entrance %s %d \n", a_sApp->window, a_sApp->window);
    }
}
